﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jenzabar.Portal.Framework;
using Jenzabar.Portal.Framework.Web.UI;
using Jenzabar.ERP;
using Jenzabar.Common.ApplicationBlocks.ExceptionManagement;
using Jenzabar.CRM.Utility;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Jenzabar.Common.Globalization;
using Jenzabar.Common.Mail;
using ConfigSettings = Jenzabar.Common.Configuration.ConfigSettings;
using Jenzabar.Portal.Framework.EmailLogging;

namespace Portlet.RHFSContract
{
    public partial class Default_View :PortletViewBase 
    {
        string major, gender, Words, act ;
        Contract obj = new Contract();
        protected void Page_Load(object sender, EventArgs e)
        {

            rbRT1.Visible = false;
            rbRT2.Visible = false;
            rbRT3.Visible = false;
            rbRT4.Visible = false;
            lblName.Text = PortalUser.Current.DisplayName;
            lblId.Text = getStudentID();// PortalUser.Current.HostID;
         
            gender = obj.findGender(lblId.Text);
            pnlRoom.Visible = false;
            if (gender == "F")
                txtGender.Text = "Female";
            else if (gender == "M")
                txtGender.Text = "Male";
            else
                txtGender.Text = "NG";

             txtDOB.Text = obj.findDOB(lblId.Text);
             DateTime  dob = Convert.ToDateTime(txtDOB.Text);
             txtAge.Text = GetAge(DateTime.Now, dob).ToString();
         
            
            
            if (IsFirstLoad)
            {

                //PopulateTerm(ddTerm);
                //ddTerm.Items.Add(new ListItem("", ""));
                //ddTerm.SelectedValue = "";
                Contract.PopulateMajor(ddMajor);
                ddMajor.Items.Add(new ListItem("Select One", " "));
                ddMajor.SelectedValue = " ";

                PopulateBuilding();
                ddBuild.Items.Add(new ListItem("Select One", " "));
                ddBuild.SelectedValue = " ";

                //rbRT1.Items.Add(new ListItem("Double Room", "DC"));
                //rbRT1.Items.Add(new ListItem("Single Room", "SC"));
                //rbRT1.Items.Add(new ListItem("Triple Room", "TR"));

                //rbRT2.Items.Add(new ListItem("Double Room", "DB"));
                //rbRT2.Items.Add(new ListItem("Single Room", "SG"));
                //rbRT2.Items.Add(new ListItem("Triple Room", "TR"));

                //rbRT3.Items.Add(new ListItem("Single Room", "SR"));
               
                
                //rbRT4.Items.Add(new ListItem("Single Room", "SU"));

                //From Room Preference Jenzabar portlet
            //    #region Custom Functions 
            //    string strXML = "";
            //    string strStudentLifeTerm = "";
            //    int intResult;
            //    if(this.ParentPortlet.Session["SLTermKey"] != null && this.ParentPortlet.Session["SLTermKey"].ToString().Length > 0)
            //        strStudentLifeTerm = this.ParentPortlet.Session["SLTermKey"].ToString().Trim();
            //    else
            //    {
            //        //Get the default PortletSettings value
            //        NameValueDataSource[] nvds;
            //        nvds = this.ParentPortlet.Settings["DefaultStudentLifeTerm"];	//RRPConstants.STUDENT_LIFE_TERM
            //        if(nvds != null && nvds.Length >0 && nvds[0].Value != null)
            //        {
            //            this.ParentPortlet.Session["SLTermKey"] = nvds[0].Value.ToString().Trim();
            //            strStudentLifeTerm = this.ParentPortlet.Session["SLTermKey"].ToString().Trim();
            //        }
            //    }

          
            //Jenzabar.ERP.Student exst = new Jenzabar.ERP.Student();
            //intResult = exst.GetPreferencesChoices(getStudentID(), strStudentLifeTerm, ref strXML);

            //#endregion


            }
        }
        private void PopulateTerm(DropDownList dd)
        {
            using (SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("SELECT [SESS_CDE] ,[SESS_DESC]  FROM [TmsEprd].[dbo].[CM_SESSION_MSTR]  Where [SESS_SESSION_SEQ]  >= 27 ORDER BY SESS_SESSION_SEQ DESC", myConn);
                //cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader rdr;

                try
                {
                    myConn.Open();
                    rdr = cmd.ExecuteReader();
                    dd.DataSource = rdr;
                    dd.DataValueField = "SESS_CDE";
                    dd.DataTextField = "SESS_DESC";
                    dd.DataBind();
                    rdr.Close();

                }
                catch (Exception ex)
                {
                    this.ParentPortlet.ShowFeedback(FeedbackType.Error, "Error retrieving Data. " + ex.Message);


                }
                finally
                {

                    myConn.Close();
                }
            }
        }
        private string getStudentID()
        {
            if (this.ParentPortlet.PortletViewState["StudentID"] == null)
                return PortalUser.Current.HostID;
            else
                return this.ParentPortlet.PortletViewState["StudentID"].ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
           
            ddBuild_SelectedIndexChanged(this, e);
            if (!ValidateAll())
            {
                ValidateAll();
            }
            else
            {
                if (Page.IsValid)
                {
                
                    setSession();
                    SaveAll();
                   sendEmail();
                    this.ParentPortlet.NextScreen("thanks");
                }


           
            }

        }

        private void sendEmail()
        {
            // Find Where the Submission Form e.g Mytest > Default Page > Email Agreement
            string strPortletID = this.ParentPortlet.ID.ToString();
            string strTabPagePortlet = string.Empty;
            string[] strParts = this.ParentPortlet.PortletDisplay.Portlet.ToString().Replace("{CN=" + this.ParentPortlet.PortletDisplay.Portlet.Name + ",", "").Replace(",CN=Portal,O=Jenzabar,C=US}", "").Replace(",OU=Pages", "").Split(',');
            for (int idx = strParts.Length; idx > 0; idx--)
            {
                strTabPagePortlet += strParts[idx - 1].Replace("CN=", "") + " > ";
            }
            strTabPagePortlet += this.ParentPortlet.Title;

            // This is the body of the Email Sent to campusSafety and Student
            // ' Form Submission<br>Where: {0}<br>When: {1}<br>Who: {2}:'  CUS_VEHICLE_REGISTRATION_MSG_RESULTS_BODY_INTRO
            string strResults = "<table border=0> <tr align= left ><th colspan=2>" +
                                    Globalizer.Format(// "CUS_AURoster_MSG_RESULTS_BODY_INTRO"
                "<table border=0 cellspacing=0 cellpadding=2><tr align= left ><td align= left colspan='2'><b>Housing Contract</b></td></tr> <tr align= left ><td align= left>Where:</td><td align= left >{0}</td></tr> <tr align= left ><td align= left >When:</td><td>{1:D} {1:t}</td></tr> <tr align= left ><td align= left >Who:</td><td>{2}</td></tr><tr align= left ><td align= left colspan='2'><hr/></td></tr></table>" 
                                   ,
                                    strTabPagePortlet,
                                    System.DateTime.Now,
                                    PortalUser.Current.NameDetails.ToString()

                                    )
                                    +
                                    "</th></tr></table>"
                                    ;

            string strToAddress = "Darby.Gough@avila.edu"; //
            string strSubject = "Housing Contract Submitted by " + lblName.Text;
            string strBody = String.Empty;
            string strFrom = PortalUser.Current.EmailAddress.ToString();
            string strValue = String.Empty;
            string strCC = "Mallika.Ramaswamy@avila.edu";//"IT@Avila.edu";
            strBody += "<table  >";
            //strBody += "<tr ><th    align=left valign=middle > New Student Details </th></tr>";
            //strBody += "<tr ><th    align=left valign=middle >Team: </th><td  align=left valign=middle >" + ddTeam.SelectedValue + "  </td> </tr> ";
            strBody += "<tr ><th    align=left valign=middle > ID# : </th><td  align=left valign=middle >" + lblId.Text + "  </td> </tr> ";
            strBody += "<tr ><th    align=left valign=middle >Name: </th><td  align=left valign=middle >" + lblName.Text + "  </td> </tr> ";
            strBody += " </table> ";
            //// Now send the results, or at least try to
            strResults += strBody;
            // filter out bad From address
            string strNoteReFailedSend = string.Empty;
            string strNoteReBadFromAddressConfig = String.Empty;
            bool blFromAddressWasGood = ((new ValidEmail(strFrom.Trim())).IsValid);
            //not good
            if (!blFromAddressWasGood)
            {
                strNoteReBadFromAddressConfig = Globalizer.GetGlobalizedString("CUS_SIMPLEFORM_MSG_BAD_FROM_ADDR_CONFIG") +
                    " " + strFrom;
                strFrom = ConfigSettings.Current.SmtpDefaultEmailAddress;
            }
            // send the message, but note if it failed
            try
            {
                //sending the email to ITService 
                if (!Email.CreateAndSendMailMessage(strFrom, strToAddress, strCC, strSubject, strResults))
                {
                    //strNoteReFailedSend += "Couldn't send to " + strToAddress + " from " + strFrom + " ";
                    strNoteReFailedSend += "Couldn't send to " + strToAddress + " from " + strFrom + " ";
                }
            }
            catch (Exception ee)
            {
                strNoteReFailedSend += ee.Message + " (see Event Log for details) - Couldn't send to " + strToAddress + " from " + strFrom + " ";

                Jenzabar.Common.ApplicationBlocks.ExceptionManagement.ExceptionManager.Publish(ee);
            }


        }

        private void SaveAll()
        {
            using (SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
            {

                try
                {
                    myConn.Open();
                     SqlCommand cmd = new SqlCommand("CUS_sp_HousingContract_Insert", myConn);

                     cmd.Parameters.Add("@name", SqlDbType.VarChar, 10).Value = lblName.Text;
                     cmd.Parameters.Add("@sid", SqlDbType.VarChar, 10).Value = lblId.Text;
                     cmd.Parameters.Add("@Age", SqlDbType.VarChar, 10).Value = txtAge.Text;
                     cmd.Parameters.Add("@dob", SqlDbType.VarChar, 10).Value = txtDOB.Text;
                     cmd.Parameters.Add("@gender", SqlDbType.VarChar, 10).Value = txtGender.Text;
                     cmd.Parameters.Add("@disablity", SqlDbType.VarChar, 10).Value = rbD.SelectedValue;
                     cmd.Parameters.Add("@class", SqlDbType.VarChar, 10).Value = rbClass.SelectedValue;
                     cmd.Parameters.Add("@term", SqlDbType.VarChar, 10).Value = ddTerm.SelectedValue;
                     cmd.Parameters.Add("@roommate ", SqlDbType.VarChar, 10).Value = rbRoommate.SelectedValue;
                     cmd.Parameters.Add("@roommate_name", SqlDbType.VarChar, 10).Value = txtRommate.Text;
                     cmd.Parameters.Add("@mealplan", SqlDbType.VarChar, 10).Value = rbMeal.SelectedValue;
                     cmd.Parameters.Add("@roomPreference", SqlDbType.VarChar, 10).Value = rbEnvir.SelectedValue;

                     cmd.Parameters.Add("@roomtype", SqlDbType.VarChar, 10).Value = Session["RT"].ToString();
                    
                    
                    cmd.Parameters.Add("@smoke", SqlDbType.VarChar, 10).Value = rbSmoke.SelectedValue;
                     cmd.Parameters.Add("@clean", SqlDbType.VarChar, 10).Value = rbClean.SelectedValue;
                     cmd.Parameters.Add("@study", SqlDbType.VarChar, 10).Value = rbListen.SelectedValue;
                     cmd.Parameters.Add("@nightowl", SqlDbType.VarChar, 10).Value = rbConsider.SelectedValue;
                     cmd.Parameters.Add("@major", SqlDbType.VarChar, 10).Value = ddMajor.SelectedValue;
                     cmd.Parameters.Add("@activity", SqlDbType.Text).Value = Session["act"].ToString();
                     cmd.Parameters.Add("@desc", SqlDbType.Text).Value = Session["Words"].ToString();
                     cmd.Parameters.Add("@comment", SqlDbType.Text).Value = txtComments.Text;

                  

                     cmd.CommandType = CommandType.StoredProcedure;
                     cmd.ExecuteNonQuery();

                }
                catch (Exception ex)
                {


                    this.ParentPortlet.ShowFeedback(FeedbackType.Error, "Error Inserting Data Items: " + ex.Message);
                }
                finally
                {
                    // Close Connection
                    myConn.Close();
                }
            }


        }

        private void setSession()
        {
            Session["sid"] = lblId.Text;
            Session["name"] = lblName.Text;
            Session["gender"] = txtGender.Text;
            Session["DOB"] = txtDOB.Text;
            Session["Age"] = txtAge.Text;
            if (rbD.SelectedValue != "")
                Session["rbD"] = rbD.SelectedItem.Text;
            else
                Session["rbD"] = "NA";

            if (rbClass.SelectedValue != "")
                Session["rbClass"] = rbClass.SelectedItem.Text;
           
            
           

            if (rbRoommate.SelectedValue == "Yes")
            {
                Session["rbRoommate"] = rbRoommate.SelectedItem.Text;
                Session["txtRommate"] = txtRommate.Text;
              
            }
            else if (rbRoommate.SelectedValue == "No")
            {
                Session["rbRoommate"] = rbRoommate.SelectedItem.Text;
                Session["txtRommate"] = " ";
            }
            if (rbMeal.SelectedValue != "")
                Session["rbMeal"] = rbMeal.SelectedItem.Text;


            if (rbEnvir.SelectedItem.Text != "")
                Session["rbEnvir"] = rbEnvir.SelectedItem.Text;

            if (ddBuild.SelectedValue.ToString().Trim() == "CAR")
            {
                Session["RT"] = rbRT1.SelectedValue.ToString();
            }
            else if (ddBuild.SelectedValue.ToString().Trim() == "RID")
            {
                Session["RT"] = rbRT2.SelectedValue.ToString();
            }
            else if (ddBuild.SelectedValue.ToString().Trim() == "R13")
            {
                Session["RT"] = rbRT3.SelectedValue.ToString();


            }
            //if (rbOccupy.SelectedItem.Text != "")
            //    Session["rbOccupy"] = rbOccupy.SelectedItem.Text;

            if (rbSmoke.SelectedValue == "Yes")
            {
                Session["rbSmoke"] = "Yes";
            }
            else if (rbSmoke.SelectedValue == "No")
            {
                Session["rbSmoke"] = "No";
            }

            if (rbClean.SelectedItem.Text != "")
                Session["rbClean"] = rbClean.SelectedItem.Text;
            
            if (rbListen.SelectedValue == "Yes")
            {
                Session["rbListen"] = "Yes";
            }
            else if (rbListen.SelectedValue == "No")
            {
                Session["rbListen"] = "No";
            }

            if (rbConsider.SelectedItem.Text != "")
                Session["rbConsider"] = rbConsider.SelectedItem.Text;

            Session["ddMajor"] = ddMajor.SelectedItem.Text;

            FindWords();
            findActivity();
      
           
       
            
            if (txtComments.Text != "")
            {
                Session["txtComments"] = txtComments.Text;
            }
            else
                Session["txtComments"] = " ";
        }


        private void findActivity()
        {
            foreach (ListItem activity in cbActivity.Items)
            {
                if (activity.Selected == true && activity.Text == "Campus Ministry")
                {
                    activity.Text = "Campus Ministry";
                    act = activity.Text + ",";
                }

                if (activity.Selected == true && activity.Text == "Mock Trials")
                {
                    activity.Text = "Mock Trials";
                    act += activity.Text + ",";
                }
               
                if (activity.Selected == true && activity.Text == "Choir")
                {
                    activity.Text = "Choir";
                    act += activity.Text + ",";
                }

                if (activity.Selected == true && activity.Text == "Theatre")
                {
                    activity.Text = "Theatre";
                    act += activity.Text + ",";
                }
               
                if (activity.Selected == true && activity.Text == "Clubs & Organizations")
                {
                    activity.Text = "Clubs & Organizations";
                    act += activity.Text + ",";
                }
                if (activity.Selected == true && activity.Text == "Publications")
                {
                    activity.Text = "Publications";
                    act += activity.Text + ",";
                }
                if (activity.Selected == true && activity.Text == "Not Sure")
                {
                    activity.Text = "Not Sure";
                    act += activity.Text + ",";
                }
                if (activity.Selected == true && activity.Text == "Avila Team Athletics")
                {

                    activity.Text = "Avila Team Athletics" + ": " + txtAthletics.Text;
                    act += activity.Text + ",";
                   
                }

                if (activity.Selected == true && activity.Text == "Other")
                {
                    
                        activity.Text = "Other" + " - " + txtOther.Text ;
                        act += activity.Text + ",";
                
                 
                }

                act += " "; 

            }

            Session["act"] = act;
        }

        private void FindWords()
        {

            int c = 0;
            
            
            
            foreach (ListItem wordItems in cb5words.Items)
            {
                if (wordItems.Selected == true && wordItems.Text == "accepts challenges")
                {
                    c++;
                    wordItems.Text = "accepts challenges";

                    Session["accepts challenges"] = "1";

                    if (c <= 5)
                        Words = wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }

                if (wordItems.Selected == true && wordItems.Text == "direct")
                {
                    wordItems.Text = "direct";

                    c++;
                    Session["direct"] = "1";

                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }


                if (wordItems.Selected == true && wordItems.Text == "great communicator")
                {
                    wordItems.Text = "great communicator";
                    c++;

                    Session["great communicator"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "logical")
                {
                    wordItems.Text = "logical";
                    c++;

                    Session["logical"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "responsible")
                {
                    wordItems.Text = "responsible";
                    c++;

                    Session["responsible"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "adventuresome")
                {
                    wordItems.Text = "adventuresome";
                    c++;

                    Session["adventuresome"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "disciplined")
                {
                    wordItems.Text = "disciplined";
                    c++;

                    Session["disciplined"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }



                }
                if (wordItems.Selected == true && wordItems.Text == "helpful")
                {
                    wordItems.Text = "helpful";
                    c++;

                    Session["helpful"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "motivated")
                {
                    wordItems.Text = "motivated";
                    c++;

                    Session["motivated"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "serious")
                {
                    wordItems.Text = "serious";
                    c++;

                    Session["serious"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "analytical")
                {
                    wordItems.Text = "analytical";
                    c++;

                    Session["analytical"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "energetic")
                {
                    wordItems.Text = "energetic";
                    c++;

                    Session["energetic"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "hates to disagree")
                {
                    wordItems.Text = "hates to disagree";
                    c++;

                    Session["hates to disagree"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "orderly")
                {
                    wordItems.Text = "orderly";
                    c++;

                    Session["orderly"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "spontaneous")
                {
                    wordItems.Text = "spontaneous";
                    c++;

                    Session["spontaneous"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "big dreamer")
                {
                    wordItems.Text = "big dreamer";
                    c++;

                    Session["big dreamer"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }



                }
                if (wordItems.Selected == true && wordItems.Text == "good at compromise")
                {
                    wordItems.Text = "good at compromise";
                    c++;

                    Session["good at compromise"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }

                if (wordItems.Selected == true && wordItems.Text == "independent")
                {
                    wordItems.Text = "independent";
                    c++;

                    Session["independent"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "patient")
                {
                    wordItems.Text = "patient";
                    c++;

                    Session["patient"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "straight to the point")
                {
                    wordItems.Text = "straight to the point";
                    c++;

                    Session["straight to the point"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "clever")
                {
                    wordItems.Text = "clever";
                    c++;

                    Session["clever"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "good listener")
                {
                    wordItems.Text = "good listener";
                    c++;

                    Session["good listener"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "likes change")
                {
                    wordItems.Text = "likes change";
                    c++;

                    Session["likes change"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "perfectionist")
                {
                    wordItems.Text = "perfectionist";
                    c++;

                    Session["perfectionist"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "tireless worker")
                {
                    wordItems.Text = "tireless worker";
                    c++;

                    Session["tireless worker"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "compassionate")
                {
                    wordItems.Text = "compassionate";
                    c++;

                    Session["compassionate"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "good sense of humor")
                {
                    wordItems.Text = "good sense of humor";
                    c++;

                    Session["good sense of humor"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "likes to argue")
                {
                    wordItems.Text = "likes to argue";
                    c++;

                    Session["likes to argue"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                        Words += "";
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }


                }
                if (wordItems.Selected == true && wordItems.Text == "quick decision maker")
                {
                    wordItems.Text = "quick decision maker";
                    c++;

                    Session["quick decision maker"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }
                if (wordItems.Selected == true && wordItems.Text == "works well with others")
                {
                    wordItems.Text = "works well with others";
                    c++;

                    Session["works well with others"] = "1";
                    if (c <= 5)
                        Words += wordItems.Text + ",";
                    else if (c > 5)
                    {
                       Words  += "  ";   wordItems.Selected = false;
                        break;
                    }

                }


                Words += "";
            }

            Session["Words"] = Words;

        }
       
        public static int GetAge(DateTime reference, DateTime birthday)
        {
            int age = reference.Year - birthday.Year;
            if (reference < birthday.AddYears(age)) age--;

            return age;
        }


        private bool ValidateAll()
        {

            if (ddBuild.SelectedValue.ToString().Trim() == "CAR")
            {
                if (rbRT1.SelectedValue.ToString().Trim() == "")
                {
                    lblERT.ForeColor = Color.Red;
                    lblERT.Text = "required!";
                    return false;
                }
                else
                {
                    lblERT.Text = "";

                }
               
            }
            else if ( ddBuild.SelectedValue.ToString().Trim() == "RID" )
            {
                if (rbRT2.SelectedValue.ToString().Trim() == "")
                {
                    lblERT.ForeColor = Color.Red;
                    lblERT.Text = "required!";
                    return false;
                }
                else
                {
                    lblERT.Text = "";

                }
            }
            else if (ddBuild.SelectedValue.ToString().Trim() == "R13")
            {
                if (rbRT3.SelectedValue.ToString().Trim() == "")
                {
                    lblERT.ForeColor = Color.Red;
                    lblERT.Text = "required!";
                    return false;
                }
                else
                {
                    lblERT.Text = "";

                }
            }
            else if (ddBuild.SelectedValue.ToString().Trim() == "JCT")
            {
                if (rbRT4.SelectedValue.ToString().Trim() == "")
                {
                    lblERT.ForeColor = Color.Red;
                    lblERT.Text = "required!";
                    return false;
                }
                else
                {
                    lblERT.Text = "";

                }
            }
           


            if (rbRoommate.SelectedValue == "Yes" && txtRommate.Text == "")
            {
                lblERoom.ForeColor = Color.Red;

                lblERoom.Text = "Name is required!";
                txtRommate.Focus();
                pnlRoom.Visible = true;
                return false;

            }
            else if (rbRoommate.SelectedValue == "Yes" && txtRommate.Text != "")
            {
                lblERoom.Text = "";

            }
            else if (rbRoommate.SelectedValue == "No")
            {
                lblERoom.Text = "";
                pnlRoom.Visible = false;

            }

            int cnt = 0;

            for (int i = 0; i < cb5words.Items.Count; i++)
            {
                if (cb5words.Items[i].Selected)
                {
                    cnt++;
                }

               
               
            }



            if (cnt < 5)
            {
                cb5wordsErr.Text = "Atleast choose 5 options";
                cb5words.Focus();
                return false;
            }
            if (cnt == 0 )
            {
               
                cb5wordsErr.Text = "Choose 5 options";
                cb5words.Focus();
                return false;
            }

            if (cnt > 6)
            {
               
                cb5wordsErr.Text = "choose only 5 options";
                cb5words.Focus();
                return false;
            }

            if (cbActivity.Items[6].Selected == true && txtAthletics.Text == "")
            {
                lblAE.Text = "Required!";
                txtAthletics.Focus();
                return false;
            }
            else
            {
                lblAE.Text = "";
              
            }
            if (cbActivity.Items[7].Selected == true && txtOther.Text == "")
            {
                lblOE.Text = "Required!";
                 txtOther.Focus();
                 return false;
            }
            else
            {
                lblOE.Text = "";
             
            }

            return true;
        }

        protected void ddMajor_SelectedIndexChanged(object sender, EventArgs e)
        {
            major = ddMajor.SelectedItem.Text;
        }

        protected void cb5words_SelectedIndexChanged(object sender, EventArgs e)
        {
            int c = cb5words.Items.Count;
            int count = 0;
            for (int i = 0; i < c; i++)
            {
                if (cb5words.Items[i].Selected == true)
                    count++;
              
            }
            if (count > 5)
            {

                for (int i = 0; i < c; i++)
                {
                    if (cb5words.Items[i].Selected == true)
                    {
                        cb5words.Items[i].Selected = false;
                        break;
                    }

                }
            }
        }

        protected void rbRoommate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rbRoommate.SelectedValue == "Yes" && txtRommate.Text == "")
            {
                lblERoom.ForeColor = Color.Red;

                lblERoom.Text = "Name is required!";
                txtRommate.Focus();
                pnlRoom.Visible = true;
               

            }
            else if (rbRoommate.SelectedValue == "No")
            {
                lblERoom.Text = "";
                pnlRoom.Visible = false;
                txtRommate.Text = "";
            }
        }

        protected void cbActivity_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        protected void ddBuild_SelectedIndexChanged(object sender, EventArgs e)
        {
            string build = ddBuild.SelectedValue.ToString().Trim();

            switch (build)
            {
                case "CAR":
                    rbRT1.Visible = true;
                    rbRT2.Visible = false;
                    rbRT3.Visible = false;
                    rbRT4.Visible = false;
                    Session["RT"] = rbRT1.SelectedValue.ToString();
                    break;
                case "RID":
                    rbRT1.Visible = false;
                    rbRT2.Visible =  true; 
                    rbRT3.Visible = false;
                    rbRT4.Visible = false;
                    Session["RT"] = rbRT2.SelectedValue.ToString();
                    break;
                case "R13":
                    rbRT1.Visible = false;
                    rbRT2.Visible = false;
                    rbRT3.Visible = true;
                    rbRT4.Visible = false;
                    Session["RT"] = rbRT3.SelectedValue.ToString();
                    
                    break;
                case "JCT":
                    rbRT1.Visible = false;
                    rbRT2.Visible = false;
                    rbRT3.Visible = false;
                    rbRT4.Visible = true;
                    Session["RT"] = rbRT4.SelectedValue.ToString();

                    break;
               
            }

        }


        private void PopulateBuilding()
        {
            using (SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
            {
                //string sql = "SELECT [BLDG_CDE] ,[BUILDING_DESC] = CASE [BUILDING_DESC]  When   'New Residence Hall Building 13' Then 'Avila Hall'  Else  [BUILDING_DESC] End  ";
                //sql += "FROM [TmsEprd].[dbo].[BUILDING_MASTER] Where BLDG_CDE in ('CAR','R13', 'RID')";
                //SqlCommand cmd = new SqlCommand(sql, myConn);

                SqlCommand cmd = new SqlCommand("CUS_sp_RHFS_Building", myConn);
               cmd.CommandType = CommandType.StoredProcedure;
                SqlDataReader rdr;

                try
                {
                    myConn.Open();
                    rdr = cmd.ExecuteReader();
                    ddBuild.DataSource = rdr;
                    ddBuild.DataValueField = "BLDG_CDE";
                    ddBuild.DataTextField = "BUILDING_DESC";
                    ddBuild.DataBind();
                    rdr.Close();

                }
                catch (Exception ex)
                {
                    this.ParentPortlet.ShowFeedback(FeedbackType.Error, "Error retrieving Data. " + ex.Message);


                }
                finally
                {

                    myConn.Close();
                }
            }
        }

        protected void ddTerm_SelectedIndexChanged(object sender, EventArgs e)
        {
            string TermEnter = ddTerm.SelectedValue.ToString().Trim();
            if (TermEnter != "")
            {

                Session["Term"] = TermEnter;
            }
            else
            {
                lblErr.Text = "Please choose the Term Enter to Avila";

            }
        }
    }
}